home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
MNetsrc.hqx
/
Mac TCP_IP Source v.33
/
mac_binsubs.c
< prev
next >
Wrap
Text File
|
1989-03-30
|
7KB
|
331 lines
/*
*
* MacBinary Subroutines.
*
*/
#include "global.h"
#include <FileMgr.h>
#include "MacBinary.h"
char *strncpy(), *malloc();
#define BLOCKS(x) ((x+127)/128)
#define lmove(f,t) movmem(f,t,4)
MBFile *MBopen( file, vrefnum, mode)
char *file;
short vrefnum,mode;
{
MBFile *mbfp;
int err;
mbfp=(MBFile *)calloc(1,sizeof(MBFile));
if (mbfp==0L)
return(0L);
movmem( file, mbfp->name,strlen(file)+1);
CtoPstr(mbfp->name);
mbfp->vrefnum=vrefnum;
mbfp->mode = mode;
if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
if ((err==-43) && (mode & MB_WRITE)) {
Create( mbfp->name, vrefnum, '????','TEXT');
if (FSOpen( mbfp->name, vrefnum, &mbfp->fd))
return( 0L);
}
else
return(0L);
}
mbfp->binary=0;
mbfp->sector1=1;
mbfp->fork=0;
mbfp->gothdr=0;
mbfp->hdrcnt=0;
setmem(&mbfp->headbuf,128,0);
return( mbfp);
}
MBwrite( out, buffer, size)
MBFile *out;
int size;
char *buffer;
{
int rsize;
char *hdpt;
rsize = size;
if(out->sector1 && !out->gothdr){
hdpt = (char *)&out->headbuf + out->hdrcnt;
if((out->hdrcnt + size) > 128){
movmem(buffer,hdpt,(128 - out->hdrcnt));
if(isMacBinary(&out->headbuf)){
ProcessMBHead(out,&out->headbuf);
buffer += (128 - out->hdrcnt);
size -= (128 - out->hdrcnt);
bwrite(out,buffer,size);
out->gothdr = 1;
return(size);
} else {
bwrite(out,&out->headbuf,128);
buffer += (128 - out->hdrcnt);
size -= (128 - out->hdrcnt);
bwrite(out,buffer,size);
return(rsize);
}
} else {
movmem(buffer,hdpt,size);
out->hdrcnt += size;
return(size);
}
}
bwrite( out,buffer,size);
return(size);
}
bwrite( out, buffer, size)
MBFile *out;
int size;
char *buffer;
{
long len=size;
if (out->binary) {
if (out->bytes>0) {
if (out->bytes< len) len = out->bytes;
FSWrite( out->fd, &len, buffer);
out->bytes -= len;
buffer +=len;
size -= (int)len;
}
if (out->bytes<= 0) {
if (!out->fork) {
out->fork=1;
out->bytes=BLOCKS(out->rlen)*128;
SetEOF( out->fd, (long) out->dlen);
FSClose( out->fd);
if (out->bytes) {
OpenRF( out->name, out->vrefnum,&out->fd);
if (size) {
len = (long) size;
FSWrite( out->fd, &len, buffer);
}
}
else
out->fd=0;
}
else SetEOF( out->fd, (long) out->rlen);
}
}
else {
FSWrite( out->fd, &len, buffer);
}
}
MBclose( out)
MBFile *out;
{
FileParam finfo;
long fpos;
if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
if (out->fork)
SetEOF( out->fd, (long) out->rlen);
else
SetEOF( out->fd, (long) out->dlen);
FSClose( out->fd);
GetFileInfo( 0, out->name, &finfo);
movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
finfo.ioFlRLgLen=out->rlen;
finfo.ioFlLgLen =out->dlen;
SetFileInfo( 0, out->name, &finfo);
}
else if (out->mode & MB_WRITE) {
GetFPos( out->fd, &fpos);
SetEOF( out->fd, fpos);
FSClose( out->fd);
}
else
FSClose( out->fd);
}
ProcessMBHead(out,header)
MBFile *out;
MBHead *header;
{
int err;
char *nptr,temp[256];
movmem( header, &out->header, sizeof(MBHead));
out->binary=1;
lmove( &header->dflen[0], &out->dlen);
lmove( &header->rflen[0], &out->rlen);
out->bytes= BLOCKS(out->dlen)*128;
out->fork=0;
out->sector1=0;
FSClose(out->fd);
if (FSDelete( out->name, out->vrefnum))
printf("mac_binsubs: Error Deleting Old File.\n");
PtoCstr(out->name);
PtoCstr(&out->header.nlen);
nptr = rindex(out->name,':');
if(nptr != NULL){
*++nptr = '\0';
sprintf(temp,"%s%s",out->name,&out->header.nlen);
} else {
sprintf(temp,"%s:%s",out->name,&out->header.nlen);
}
movmem(temp,out->name,strlen(temp)+1);
CtoPstr(out->name);
CtoPstr(&out->header.nlen);
MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
if (out->bytes) {
if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
if (err=-43) {
Create( out->name, out->vrefnum, '????','TEXT');
if (FSOpen( out->name, out->vrefnum, &out->fd))
return( 0L);
}
else {
return(0L);
}
}
}
else {
if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
if (err=-43) {
Create( out->name, out->vrefnum, '????','TEXT');
if (OpenRF( out->name, out->vrefnum, &out->fd))
return( 0L);
}
else {
return(0L);
}
}
out->fork = 1;
out->bytes=BLOCKS(out->rlen)*128;
}
}
MBread( in, buffer, size)
MBFile *in;
int size;
char *buffer;
{
char *p,*nptr;
int rsize=size;
if (in->fork<0) {
return(-1);
}
p=buffer;
if (in->sector1) {
FileParam finfo;
setmem( &in->header, sizeof(MBHead), 0);
PtoCstr(in->name);
nptr = rindex(in->name,':');
if(nptr != NULL){
nptr++;
movmem( nptr, &in->header.nlen, strlen(nptr)+1);
} else {
movmem(in->name,&in->header.nlen,strlen(nptr)+1);
}
CtoPstr(in->name);
CtoPstr(&in->header.nlen);
GetFileInfo( in->vrefnum, in->name, &finfo);
movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
in->header.protected = (in->header.zero2 & 0x40)?1:0;
in->header.zero2 = 0;
lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
in->dlen=finfo.ioFlLgLen;
in->rlen=finfo.ioFlRLgLen;
if (! (in->mode & MB_DISABLE) ) {
if (size<128) return(-1);
movmem( &in->header, p, 128);
p +=128;
size -= 128;
in->bytes= BLOCKS(in->dlen)*128;
in->binary=1;
}
else {
in->bytes = in->dlen;
in->rlen=0;
in->binary=0;
}
in->sector1=0;
MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
}
if ( size >0) {
long length = (long)size;
int err;
err = FSRead( in->fd, &length, p);
size -=(int)length;
in->bytes -=length;
p += length;
if (err == -39 || (in->bytes<=0) ) {
if (in->bytes<0L) in->bytes=0L;
size -= (int)in->bytes;
p += in->bytes; /* Make adjustments for necessary 128 byte term */
if (!in->fork ) {
in->fork=1;
in->bytes= BLOCKS(in->rlen)*128;
FSClose( in->fd);
if (in->bytes) {
OpenRF( in->name, in->vrefnum, &in->fd);
length=(long)size;
if (length >0L) {
err = FSRead( in->fd, &length, p);
size -= (int)length;
in->bytes -=length;
}
}
else {
in->fd=0;
in->fork=-1; /* Time to close up shop */
}
}
else {
in->fd=0;
in->fork=-1; /* Time to close up shop */
}
}
}
return( rsize-size);
}
isMacBinary(p)
MBHead *p;
{
return( (p->zero1 == 0) &&
(p->zero2 == 0) &&
(p->zero3 == 0));
}
MBstat()
{
return(0);
}